/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.dao.ICheckConfirmItemConfigDao;
import com.ce.pms.system.renovation.model.CheckConfirmItemConfigDO;
import com.ce.pms.system.renovation.service.ICheckConfirmItemConfigService;
import com.ce.pms.system.renovation.vo.CheckConfirmItemConfigVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：<br>
 * @author 易贤建 创建日期：2008-12-25<br>
 */
public class CheckConfirmItemConfigServiceImpl extends PmsBaseServiceImpl implements
		ICheckConfirmItemConfigService {

	private ICheckConfirmItemConfigDao checkConfirmItemConfigDao = null;

	public CheckConfirmItemConfigVO add(CheckConfirmItemConfigVO configVO) {
		checkParameterIsNull(configVO, "system:approval_info_not_null"); //填入基本信息
		checkParameterIsNull(configVO.getItemName(),"system:approvalItem_info_item_not_null");
		validateItemName(configVO);

		CheckConfirmItemConfigDO configDO = assembleV2D(configVO);
		configDO.setSysInfo(super.createInfo(configVO.getCreator()));
		configDO.setState("001");
		CheckConfirmItemConfigDO newDO = checkConfirmItemConfigDao.saveObject(configDO);
		return assembleD2V(newDO);
	}

	public void delete(Long keyword) {
		checkParameterIsNull(keyword,"system:approvalItemConfig_delte_ID_NULL");
		CheckConfirmItemConfigDO configDO = checkConfirmItemConfigDao.findById(keyword);
		checkParameterIsNull(configDO,"system:approvalItemConfig_DO_NOT_FOUND");
		checkConfirmItemConfigDao.deleteObject(configDO);
	}

	public CheckConfirmItemConfigVO edit(CheckConfirmItemConfigVO configVO) {
		checkParameterIsNull(configVO,"system:approval_info_not_null");
		checkParameterIsNull(configVO.getKeyword(),"system:approvalItemConfig_delte_ID_NULL");
		checkParameterIsNull(configVO.getItemName(),"system:approval_item_not_null");
		validateItemName(configVO);

		CheckConfirmItemConfigDO configDO = checkConfirmItemConfigDao.findById(configVO.getKeyword());
		checkParameterIsNull(configDO,"system:approvalItemConfig_DO_NOT_FOUND");

		configDO.setItemName(configVO.getItemName());
		configDO.setState("001");
		configDO.setSysInfo(super.createInfo(configVO.getModifier()));
		return assembleD2V(configDO);
	}

	public CheckConfirmItemConfigVO findById(Long keyword) {
		CheckConfirmItemConfigDO configDO = checkConfirmItemConfigDao.findById(keyword);
		return assembleD2V(configDO);
	}

	@SuppressWarnings("unchecked")
	public Page queryCheckConfirmItemList(SearchVO searchVO) {
		Page page = checkConfirmItemConfigDao.queryAll(searchVO);
		List result = page.getResult();
		page.setResult(assembleD2V(result));
		return page;
	}

	/**
	 * 功能描述:查询所有的可用的收费项目，不分页<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<CheckConfirmItemConfigVO> queryCheckConfirmItemList(){
		List<CheckConfirmItemConfigDO> itemConfigList= checkConfirmItemConfigDao.queryAll();
		return assembleD2V(itemConfigList);
	}

	/**
	 * 功能描述:验证装受收费项目<br>
	 * @param configVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public void validateItemName(CheckConfirmItemConfigVO configVO){
		if(configVO == null) return ;
		if(BlankUtil.isBlank(configVO.getItemName())) return ;

		Long keyword = configVO.getKeyword();
		String itemName = configVO.getItemName();

		List<CheckConfirmItemConfigDO> appDOS = null;
		if(keyword == null){
			appDOS = checkConfirmItemConfigDao.findByItemName(itemName);
		}else{
			appDOS = checkConfirmItemConfigDao.findByItemNameWithoutKeyword(keyword,itemName);
		}
		if(!BlankUtil.isBlank(appDOS))
			throw new BusinessException("system:checkConfirm_itemName_isExist",BusinessException.WARNING);
	}
	private CheckConfirmItemConfigDO assembleV2D(CheckConfirmItemConfigVO configVO){
		if(configVO == null) return null;
		CheckConfirmItemConfigDO configDO = new CheckConfirmItemConfigDO();
		configDO.setKeyword(configVO.getKeyword());
		configDO.setItemName(configVO.getItemName());

		return configDO;
	}
	/**
	 * 功能描述:DO<br>
	 * @param configDOS
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private List<CheckConfirmItemConfigVO> assembleD2V(List<CheckConfirmItemConfigDO> configDOS){
		if(BlankUtil.isBlank(configDOS)) return null;
		List<CheckConfirmItemConfigVO> configVOS = new ArrayList<CheckConfirmItemConfigVO>();
		for(CheckConfirmItemConfigDO configDO : configDOS){
			configVOS.add(assembleD2V(configDO));
		}
		return configVOS;
	}
	/**
	 * 功能描述:  <br>
	 * @param configDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private CheckConfirmItemConfigVO assembleD2V(CheckConfirmItemConfigDO configDO){
		if(configDO == null) return null;
		CheckConfirmItemConfigVO configVO = new CheckConfirmItemConfigVO();
		configVO.setKeyword(configDO.getKeyword()); //keyword
		configVO.setItemName(configDO.getItemName()); //确认项目
		return configVO;
	}

	public void setCheckConfirmItemConfigDao(
			ICheckConfirmItemConfigDao checkConfirmItemConfigDao) {
		this.checkConfirmItemConfigDao = checkConfirmItemConfigDao;
	}

}
